﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data.Service
{
    public class CategoryData
    {
        public static SYTDataBaseEntities db = new SYTDataBaseEntities();

        //查询类目集合
        public static List<Category> GetCategory()
        {
            return db.Category.ToList();
        }
        //通过父id获取类目
        public static List<Category> GetCategory(string Id)
        {
            return db.Category.Where(M => M.ParentCategoryId == Id).ToList();
        }
        //通过Id获取类目
        public static Category GetCategorySingle(string pId)
        {
            return db.Category.Where(c => c.categoryID == pId).Single();
        }
        //关键字搜索类目 
        public static List<string> GetCategoryByKeyword(string keyword)
        {
            List<string> strs = new List<string>();
            string str = "";
            Category cBig = new Category();
            Category cMiddle = new Category();

            List<Category> Categorys = db.Category.Where(c => c.categoryName.Contains(keyword)).ToList();

            if (Categorys.Count > 0)
            {
                foreach (var item in Categorys)
                {
                    if (item.biaoshi == 1)
                    {
                        foreach (var c1 in GetCategory(item.categoryID))
                        {
                            foreach (var c2 in GetCategory(c1.categoryID))
                            {
                                str = item.categoryName + ">" + c1.categoryName + ">" + c2.categoryName;
                                strs.Add(str);
                            }
                        }
                    }
                    else if (item.biaoshi == 2)
                    {
                        foreach (var c1 in GetCategory("0"))
                        {
                            if (item.ParentCategoryId == c1.categoryID)
                            {
                                foreach (var c2 in GetCategory(item.categoryID))
                                {
                                    str = c1.categoryName + ">" + item.categoryName + ">" + c2.categoryName;
                                    strs.Add(str);
                                }
                            }
                        }
                    }
                    else if (item.biaoshi == 3)
                    {
                        cMiddle = GetCategorySingle(item.ParentCategoryId);
                        cBig = GetCategorySingle(cMiddle.ParentCategoryId);
                        str = cBig.categoryName + ">" + cMiddle.categoryName + ">" + item.categoryName;
                        strs.Add(str);
                    }
                }
            }


            return strs.Take(30).ToList();
        }

    }
}
